export let snakes = [];

export default class Snake {
    oldPosition = {};
    constructor(app, options) {
        this.app = app;
        this.options = Object.assign(
            {
                bgColor: 'green',
            },
            options
        );

        this.el = this.element();

        snakes.push(this);
    }

    //创建蛇
    element() {
        let div = document.createElement('div');
        div.classList.add('snake');
        div.style.backgroundColor = this.options.bgColor;
        div.style.left = this.options.left + 'px';
        div.style.top = this.options.top + 'px';

        return div;
    }

    set left(value) {
        this.oldPosition.left = this.left;
        this.el.style.left = value + 'px';
    }

    set top(value) {
        this.oldPosition.top = this.top;
        this.el.style.top = value + 'px';
    }

    get left() {
        return this.el.offsetLeft;
    }

    get top() {
        return this.el.offsetTop;
    }

    static get first() {
        return snakes[0];
    }

    static get last() {
        return snakes[snakes.length - 1];
    }

    static get count() {
        return snakes.length-1;
    }

    static remove() {
        while (snakes.length >0){
            let snake = snakes.pop();
            snake.el.remove();
        }
    }
}
